#include "JDNNGameScence.h"
#include "Game/GameLib.h"

#include "Main/stdafx.h"
#include "CMD_Ox.h"
#include "JDNNPlayer.h"
#include "Games/NiuNiuGameLogic.h"
#include "GameLib/JniCross/JniFun.h"
#include "Game/Game/MissionWeiXin.h"

#include "Games/GameChatLayer.h"
#include "JDNNSoundFun.h"
#include "Main/configConst.h";
#include "Games/SetSoundLayer.h" 

#include "JDNNZhanJiRecordLayer.h"

void JDNNGameScence::initButton()
{
	Layout* helpLayout = (Layout*)_layout->getChildByName("Panel_help");
	helpLayout->addClickEventListener([=](Ref* pSender){
		helpLayout->setVisible(false);
	});

	auto btn = (Button*)_layout->getChildByName("Button_ready");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));

	btn = (Button*)_layout->getChildByName("Button_addFriend");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_back");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_dismiss");
	//btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	//btn = (Button*)_layout->getChildByName("Button_exit");
	//btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	//btn = (Button*)_layout->getChildByName("Button_help");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_chat");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_layout->getChildByName("Button_talk");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));

	btn = (Button*)_layout->getChildByName("Button_group");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	m_btnGroup = btn;

	_panelGroup = (Layout*)_layout->getChildByName("Panel_group");
	btn = (Button*)_panelGroup->getChildByName("Button_exit");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_panelGroup->getChildByName("Button_music");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
	btn = (Button*)_panelGroup->getChildByName("Button_zhanji");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));


	for (int i = 0; i < BTN_MAX; ++i)
	{
		btn = (Button*)_layout->getChildByName(StringUtils::format("Button_opt%d",i));
		btn->addTouchEventListener(CC_CALLBACK_2(JDNNGameScence::BtnCallBack, this));
		m_btns[i] = btn;
	}
}

void JDNNGameScence::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		JDNNSoundFun::playEffect("effect_click");
		if (btn->getName().compare("Button_ready") == 0)
		{
			sendReady();
			defaultState();
		}
		else if (btn->getName().compare("Button_addFriend") == 0)
		{
			auto txt = (Text*)_layout->getChildByName("Sprite_table")->getChildByName("Text_deskNo");
			//MissionWeiXin::Instance().shareTextWeiXin(utility::getScriptReplaceValue("PrivateTxt1", txt->getString()));
			// http://www.rdaohy.com/
			std::string url = StringUtils::format("%s/admin/share.aspx", bzyfk_url.c_str());
			MissionWeiXin::Instance().shareUrlWeiXin(url, utility::getScriptString("PrivateTxt4"), utility::getScriptReplaceValue("PrivateTxt1", txt->getString()));
		}
		else if (btn->getName().compare("Button_back") == 0 || btn->getName().compare("Button_exit") == 0)
		{
			Button_ExitGameBase();
			CGlobalUserInfo::GetInstance()->isBackRoom = true;
		}
		else if (btn->getName().compare("Button_music") == 0)
		{
			SetSoundLayer* soundLayer = SetSoundLayer::create();
			addChild(soundLayer);
		}
		else if (btn->getName().compare("Button_zhanji") == 0)
		{
			CCLOG("Button_zhanji");
			int userId = UserInfo::Instance().getUserID();
			CMD_C_HistoryScore histroyScore;
			histroyScore.dwUserID = userId;
			SendSocketData(SUB_C_HISTORY_SCORE, &histroyScore, sizeof(histroyScore));

			if (m_ZhanJiRecordLayer)
			{
				m_ZhanJiRecordLayer->showZhanJiRecordLayer();
			}
		}
		else if (btn->getName().compare("Button_group") == 0)
		{
			if (!_panelGroup->isVisible())
			{
				_panelGroup->setVisible(true);
				m_btnGroup->loadTextureNormal("game2/ui_bairenniuniu_icon2.png");
			}
			else
			{
				_panelGroup->setVisible(false);
				m_btnGroup->loadTextureNormal("game2/ui_bairenniuniu_icon1.png");
			}
		}
		else if (btn->getName().compare("Button_dismiss") == 0)
		{
			CMD_GR_Dismiss_Private kNetInfo;
			kNetInfo.bDismiss = 1;
			CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			/*
			auto dismissDialog = CSLoader::createNode("aqmj/commonLayer.csb");
			addChild(dismissDialog, 201, 201);
			dismissDialog->setName("disMissDialog");

			Button* btn = (Button*)dismissDialog->getChildByName("Button_cancel");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);
			});

			btn = (Button*)dismissDialog->getChildByName("Button_confirm");
			btn->addClickEventListener([=](Ref* pSender){
				dismissDialog->removeFromParentAndCleanup(true);

				CMD_GR_Dismiss_Private kNetInfo;
				kNetInfo.bDismiss = 1;
				CServerItem::get()->SendSocketData(MDM_GR_PRIVATE, SUB_GR_PRIVATE_DISMISS, &kNetInfo, sizeof(kNetInfo));
			});
			*/
			CGlobalUserInfo::GetInstance()->isBackRoom = false;
		}
		else if (btn->getName().compare("Button_help") == 0)
		{
			Layout* helpLayout = (Layout*)_layout->getChildByName("Panel_help");
			helpLayout->setVisible(true);
		}
		else if (btn->getName().compare("Button_chat") == 0)
		{
			GameChatLayer* gChatLayer = GameChatLayer::create();
			addChild(gChatLayer);
			gChatLayer->showRightPos();
		}
		else if (btn->getName().compare("Button_talk") == 0)
		{
			Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
			if (sprTalk->isVisible())
			{
				JniFun::stopSoundRecord();
				sprTalk->setVisible(false);
				return;
			}

			SoundFun::SetSoundBackground(false);
			cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_SOUND_VALUE", false);		
		    UserDefault::getInstance()->flush();

			sprTalk->setVisible(true);
			JniFun::startSoundRecord();
			int iTimeID = TimeManager::Instance().addCerterTimeCB(TIME_CALLBACK(JDNNGameScence::endButtonTalk, this), 5.0f)->iIdex;
			sprTalk->setUserData((void*)iTimeID);
		}
		else if (btn->getName().compare("Button_opt0") == 0)
		{
			CMD_C_CallBanker callZhuang;
			callZhuang.bBanker = true;
			SendSocketData(SUB_C_CALL_BANKER, &callZhuang, sizeof(callZhuang));

			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt1") == 0)
		{
			CMD_C_CallBanker callZhuang;
			callZhuang.bBanker = false;
			SendSocketData(SUB_C_CALL_BANKER, &callZhuang, sizeof(callZhuang));

			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt2") == 0)
		{
			CMD_C_AddScore scoreInfo;
			scoreInfo.lScore = 1;// MAX(m_addTotalScore / 4, 1L);
			SendSocketData(SUB_C_ADD_SCORE, &scoreInfo, sizeof(scoreInfo));
			log("====================1==>send score:%lld", scoreInfo.lScore);
			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt3") == 0)
		{
			CMD_C_AddScore scoreInfo;
			scoreInfo.lScore = 3;// MAX(m_addTotalScore / 3, 1L);
			SendSocketData(SUB_C_ADD_SCORE, &scoreInfo, sizeof(scoreInfo));
			log("====================2==>send score:%lld", scoreInfo.lScore);
			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt4") == 0)
		{
			CMD_C_AddScore scoreInfo;
			scoreInfo.lScore = 5;// MAX(m_addTotalScore / 2, 1L);
			SendSocketData(SUB_C_ADD_SCORE, &scoreInfo, sizeof(scoreInfo));
			log("====================3==>send score:%lld", scoreInfo.lScore);
			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt5") == 0)
		{
			CMD_C_AddScore scoreInfo;
			scoreInfo.lScore = 10;// MAX(m_addTotalScore / 1, 1L);
			SendSocketData(SUB_C_ADD_SCORE, &scoreInfo, sizeof(scoreInfo));
			log("====================4==>send score:%lld", scoreInfo.lScore);
			hiddenAllBtns();
		}
		else if (btn->getName().compare("Button_opt6") == 0)
		{
			Node* niuPanel = (Node*)_layout->getChildByName("game_niuniu_panel");
			TextAtlas* textAtlas = (TextAtlas*)niuPanel->getChildByName("AtlasLabel_4");
			int totalValue = atoi(textAtlas->getString().c_str());
			if (totalValue >= 10 && (totalValue % 10 == 0))
			{
				CMD_C_OxCard niuInfo;
				niuInfo.bOX = true;
				SendSocketData(SUB_C_OPEN_CARD, &niuInfo, sizeof(niuInfo));

				hiddenAllBtns();
			}
		}
		else if (btn->getName().compare("Button_opt7") == 0)
		{
			CMD_C_OxCard niuInfo;
			niuInfo.bOX = false;
			SendSocketData(SUB_C_OPEN_CARD, &niuInfo, sizeof(niuInfo));

			hiddenAllBtns();
			/*
			Node* niuPanel = (Node*)_layout->getChildByName("game_niuniu_panel");
			TextAtlas* textAtlas = (TextAtlas*)niuPanel->getChildByName("AtlasLabel_4");
			int totalValue = atoi(textAtlas->getString().c_str());
			if (totalValue % 10 != 0)
			{
				CMD_C_OxCard niuInfo;
				niuInfo.bOX = false;
				SendSocketData(SUB_C_OPEN_CARD, &niuInfo, sizeof(niuInfo));

				hiddenAllBtns();
			}
			*/
		}
		else if (btn->getName().compare("Button_continue") == 0)
		{
			_layout->getChildByName("Panel_result")->setVisible(false);
			sendReady();
			defaultState();
		}
		else if (btn->getName().compare("Button_share") == 0)
		{
			static JDNNGameScence* pTemp = NULL;
			pTemp = this;
			cocos2d::utils::captureScreen([](bool b, std::string name){
				if (b)
				{
					MissionWeiXin::Instance().shareScreenWeiXin(name);
					pTemp->Button_ExitGameBase();
					pTemp->ExitGame();
					pTemp->_layout->getChildByName("finish_node")->setVisible(false);
				}
			}, "screenshot.png");
		}
	}
		break;
	default:
		break;
	}
}

void JDNNGameScence::endButtonTalk()
{
	Sprite* sprTalk = (Sprite*)_layout->getChildByName("sprite_talk");
	if (!sprTalk->isVisible())
	{
		return;
	}
	int iTimeID = (uintptr_t) sprTalk->getUserData();
	TimeManager::Instance().removeByID(iTimeID);
	sprTalk->setVisible(false);
	std::string kFileName = JniFun::stopSoundRecord();

	SoundFun::SetSoundBackground(true);
	cocos2d::UserDefault::getInstance()->setBoolForKey("JDNN_SOUND_VALUE", true);
	UserDefault::getInstance()->flush();

	log("-------send talk file Name:%s", kFileName.c_str());
	sendTalkFile(m_pLocal->GetChairID(), kFileName);
}
void JDNNGameScence::BtnHeadCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		int userIndex = (intptr_t) btn->getUserData();
		JDNNPlayer* pPlayer = m_pPlayer[userIndex];
		MessageBox("show userInfo<===========", "title");
// 		PopScence::Instance().showIPInfo(pPlayer->GetNickName(),
// 			utility::paseInt(pPlayer->GetUserID(), 6),
// 			pPlayer->GetUserInfo()->szLogonIP,
// 			pPlayer->GetUserInfo()->szHeadHttp);
	}
		break;
	default:
		break;
	}
}
